﻿using Candy.Model;
using Candy.Web.Data;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace Candy.ViewModel
{
    public class AnnotationGroupViewModel : CandyViewModelBase
    {
        private GeneEditViewModel _geneViewModel;

        public AnnotationType Type { get; set; }


        private string _title;
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        private ObservableCollection<GeneAnnotationEditViewModel> _annotations;
        public ObservableCollection<GeneAnnotationEditViewModel> Annotations
        {
            get
            {
                return _annotations;
            }
            set
            {
                _annotations = value;
                RaisePropertyChanged("Annotations");
            }
        }


        private IDataService _dataService;
        public AnnotationGroupViewModel(IDataService dataService, GeneEditViewModel geneVM, AnnotationType type)
        {
            Type = type;
            Title = type.ToString();
            Annotations = new ObservableCollection<GeneAnnotationEditViewModel>();
            _dataService = dataService;
            _geneViewModel = geneVM;

            if (!IsInDesignMode)
            {
                _geneViewModel.Entity.Annotations.EntityAdded += Annotations_EntityAdded;
                _geneViewModel.Entity.Annotations.EntityRemoved += Annotations_EntityRemoved;
            }
        }

        void Annotations_EntityRemoved(object sender, System.ServiceModel.DomainServices.Client.EntityCollectionChangedEventArgs<GeneAnnotationDto> e)
        {
            if (e.Entity.Annotation == Type)
            {
                var vm = Annotations.SingleOrDefault(x => x.Entity == e.Entity);
                if (vm != null)
                    Annotations.Remove(vm);
            }
        }

        void Annotations_EntityAdded(object sender, System.ServiceModel.DomainServices.Client.EntityCollectionChangedEventArgs<GeneAnnotationDto> e)
        {
            if (e.Entity.Annotation == Type)
            {
                GeneAnnotationEditViewModel vm;
                if (Type == AnnotationType.Reference)
                {
                    vm = new GeneReferenceEditViewModel(_dataService, e.Entity);
                }
                else
                {
                    vm = new GeneAnnotationEditViewModel(_dataService, e.Entity);
                }

                Annotations.Insert(0, vm);
                if (vm.IsNewEntitiy)
                    vm.Edit.Execute(null);
            }
        }


        private RelayCommand _addAnnotation;
        public RelayCommand AddAnnotation
        {
            get
            {
                return _addAnnotation ?? (_addAnnotation = new RelayCommand(ExecAddAnnotation, CanAddAnnotation));
            }
        }

        /// <summary>
        /// Checks whether the AddAnnotation command is executable
        /// </summary>
        private bool CanAddAnnotation()
        {
            return true;
        }

        /// <summary>
        /// Executes the AddAnnotation command 
        /// </summary>
        private void ExecAddAnnotation()
        {
            if (Annotations == null)
                Annotations = new ObservableCollection<GeneAnnotationEditViewModel>();
            GeneAnnotationDto dto = new GeneAnnotationDto();
            dto.ProjectId = _dataService.LoadedProject.Id;
            dto.GeneId = _geneViewModel.Entity.Id;

            dto.Annotation = Type;
            dto.CreateDate = DateTime.Now;
            dto.ModifiedDate = DateTime.Now;
            _geneViewModel.Entity.Annotations.Add(dto);
            //GeneAnnotationEditViewModel vm = new GeneAnnotationEditViewModel(_dataService, dto);
            //this.Annotations.Insert(0, vm);
            //vm.Edit.Execute(null);
        }



    }
}
